
// var sysctlbyname = getExportFunction("f", "sysctlbyname", "int",
//     ["pointer", "pointer", "pointer", "pointer", "int"]);
// var sysctl = getExportFunction("f", "sysctl", "int",
//     ["pointer", "int", "pointer", "pointer", "pointer", "int"]);

// let nptr = Module.findExportByName(null, 'sysctlbyname');
// Interceptor.attach(nptr, {
//     onEnter(args){
//         let arg0 = args[0];
//         let arg1 = args[1];
//         let arg2 = args[2];
//         let arg3 = args[3];
//         this.name = Memory.readUtf8String(arg0)
//         this.buf = arg1;
//         this.bufsize = arg2;
//         // console.log(arg0, arg1, arg2, arg3)
//
//     },
//     onLeave(ret){
//         console.log('onLeave',this.name,!this.buf.equals(0)? this.buf.readU16():'error', Memory.readUtf8String(this.buf))
//         // console.log(Thread.backtrace(this.context, Backtracer.ACCURATE)
//         //     .map(DebugSymbol.fromAddress).join('\n') + '\n')
//     }
// })
var NSString = ObjC.classes.NSString;

function getNSString(str) {
    return NSString.stringWithUTF8String_(Memory.allocUtf8String(str));
}


// let appKey = getNSString('9101430221728_IOS')
// let api = getNSString('alipay.client.updateVersion')
// let signData = getNSString('Operation-Type=alipay.client.updateVersion&Request-Data=AwAAIQKam1t89igK9npOsZxIjK6jrKbbIuUJZXroLaNjg1hJVA0AAPBY0pwCzzaUGTpmF+udid85IDj8sA8k+E5fFyAVz/NyGas8d8Cog4f97Zobnr5OoEd7yUNVkn8o+UGGmr5HsGKph1gH4oBZwVaPhcE/VDU0Cg/W5s3Bc7FpSDu9BvjOfWuwOAgPEoSNndCuBWQntIcHDvR7kGfh0nQ8xxwzkIOPkzgt4NOE0T+Z+u6BDJ6r0bOzRlUIULWfREZQcvkV0MI6fpOKQej3oh85VypYhuD+67+XVWrYz2Nn9SIaWWF1ifkUZe+ueouH17OlCcjlR+epEVw/kxaAYtzwUDG3nK7A/ucw0eEJR7sY8jwc7aOwubE=&Ts=PM3iDI7')
//
// let a = ObjC.classes['MPSignatureService']['sharedInstance']();
// let r = a['trustedSignBySignKey:signApi:content:'](appKey, api, signData)
//
// let color =  r['- objectForKey:'](getNSString('x-djy-color'));
// console.log(color['- cStringUsingEncoding:'](4))


let info_ids = [];
Interceptor.attach(
    Module.getExportByName(
        null,
        "open"
    ), {
        onEnter: function (args) {
            // 读取路径参数
            const pathPtr = args[0];
            this.path = pathPtr.readUtf8String();

            // 输出监控信息
            // console.log(`[+] stat called for: ${this.path}`);
        },
        onLeave: function (retval) {
            // console.log(`[+] open called for: ${this.path} => ${retval}`);
            if (this.path.includes('aa12306.app/Info.plist')){
                info_ids.push(retval.toInt32())
            }
            // 可以在这里修改返回值
            // retval.replace(0); // 强制返回成功
        }
    });

// Interceptor.attach(
//     Module.getExportByName(
//         null,
//         "access"
//     ), {
//         onEnter: function (args) {
//             // 读取路径参数
//             const pathPtr = args[0];
//             this.path = pathPtr.readUtf8String();
//
//             // 输出监控信息
//             // console.log(`[+] stat called for: ${this.path}`);
//         },
//         onLeave: function (retval) {
//             console.log(`[+] access called for: ${this.path} => ${retval}`);
//             // 可以在这里修改返回值
//             // retval.replace(0); // 强制返回成功
//         }
//     });

Interceptor.attach(
    Module.getExportByName(
        null,
        "read"
    ), {
        onEnter: function (args) {
            // 读取路径参数
            this.fd = args[0];
            this.buffer = args[1];
            this.size = args[2].toInt32();
            this.sizePtr = args[2];

            // 输出监控信息
            // console.log(`[+] stat called for: ${this.path}`);
        },
        onLeave: function (retval) {
            if (this.fd && this.size > 0 && retval.toInt32() > 0 && info_ids.includes(this.fd.toInt32())) {
                // 读取成功，打印缓冲区内容
                var data = Memory.readByteArray(this.buffer, retval.toInt32());
                console.log('[*] read from fd ' + this.fd +'len' +retval.toInt32() + ':');
                // console.log(hexdump(data, { offset: 0, length: retval.toInt32(), header: false }));
                info_ids = [];
                // 尝试将数据转换为字符串
                var str = Memory.readUtf8String(this.buffer, retval.toInt32());
                console.log(1,str.length)
                str = str.replaceAll('cn.12306.aa12306', 'cn.12306.rails12306')
                console.log(str)

                console.log(str.length)
                this.buffer.writeUtf8String(str)
                // retval.replace(ptr(retval.add(3)))
                retval.replace(str.length)
                console.log(`[*] read2 from fd this.size ${this.size} ` + this.fd +'len' +retval.toInt32() + ':');

                // if (str.includes('CFBundleIdentifier')) {
                //     console.log('[*] Found CFBundleIdentifier in: ' + str);
                // }
            }
        }
    });